package llama.view.panels;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Desktop;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkEvent.EventType;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultCaret;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;

import llama.core.IUser;
import llama.util.ColorHelper;

public class ChatTextPanel extends JPanel implements HyperlinkListener {
	private HTMLDocument doc;
	private HTMLEditorKit htmlKit;
	private JTextPane textPane;
	
	private IUser user;
	
	/**
	 * Create the panel.
	 */
	public ChatTextPanel(final IUser user) {
		setLayout(new BorderLayout(0, 0));
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setAutoscrolls(true);
		add(scrollPane, BorderLayout.CENTER);
		
		textPane = new JTextPane();
		textPane.addHyperlinkListener(this);
		textPane.setEditable(false);
		htmlKit = new HTMLEditorKit();
	    doc = new HTMLDocument();
	    textPane.setEditorKit(htmlKit);
	    textPane.setDocument(doc);
		
		DefaultCaret caret = (DefaultCaret)textPane.getCaret();
		caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
		
		textPane.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				super.focusLost(e);
				textPane.setCaretPosition(doc.getLength());
			}
		});
		
		scrollPane.setViewportView(textPane);
		
		this.user = user;
	}
	
	public IUser getUser() {
		return user;
	}
			
	public void appendText(String text) {
		appendText("", "", Color.black, text);
	}
	
	public void appendText(String time, String alias, Color aliasColor, String message) {
		appendText(time, alias, aliasColor, message, Color.black);
	}
	
	public void appendText(String time, String alias, Color aliasColor, String message, Color messageColor) {
		String text = "<font color=\"#"+ColorHelper.colorToHex(aliasColor)+"\">"+time + alias+": </font>"+
		"<font color=\"#"+ColorHelper.colorToHex(messageColor)+"\">"+message+"</font>";
		
		insertHTML(text);
	}
	
	private void insertHTML(String text) {
		try {
			htmlKit.insertHTML(doc, doc.getLength(), text, 0, 0, null);
		} catch (BadLocationException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Intended for text pre-processed by a Text-plugin.
	 * @param text - Text to be directly appended to text pane.
	 */
	public void appendHTMLFormattedText(String text) {
		insertHTML(text);
	}

	@Override
	public void hyperlinkUpdate(HyperlinkEvent e) {
		if(e.getEventType() == EventType.ACTIVATED && e.getURL() != null) {
			try {
				URI uri = e.getURL().toURI();
				Desktop.getDesktop().browse(uri);
			} catch (IOException e1) {
				e1.printStackTrace();
			} catch (URISyntaxException e2) {
				e2.printStackTrace();
			}
		}
	}

}
